import 'package:get_storage/get_storage.dart';

class Storage {
  static late GetStorage _gs;

  static Future init({String title = "JmApp"}) async {
    await GetStorage.init(title);
    _gs = GetStorage(title);
  }

  static Future<void> write(
    String key,
    dynamic value,
  ) async {
    return _gs.write(key, value);
  }

  static T read<T>(String key, {required T defVal}) {
    T? v = _gs.read(key);
    if (v == null) {
      return defVal;
    }
    return v;
  }
}

class MyStorage {
  final String container;
  late GetStorage _gs;

  MyStorage(this.container, [String? path, Map<String, dynamic>? initialData]) {
    _gs = GetStorage(container, path, initialData);
  }

  Future init() async {
    await GetStorage.init(container);
  }

  Future<bool> get isReady => _gs.initStorage;
  List<String> get keys => _gs.getKeys().toList();

  erase() => _gs.erase();

  Future<void> write(
    String key,
    dynamic value,
  ) {
    return _gs.write(key, value);
  }

  T? read<T>(String key, {T? defVal}) {
    T? v = _gs.read<T>(key);
    if (v == null && defVal != null) {
      return defVal;
    }
    return v;
  }

  // Future<void> _init(String container,[
  //   String? path,
  //   Map<String, dynamic>? initialData]) async {
  //   await GetStorage.init(container);
  // }
}
